import 'package:cached_network_image/cached_network_image.dart';
import 'package:card_swiper/card_swiper.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/core/common/model/banner_model.dart';
import 'package:flutter_demo/core/common/values/server.dart';
import 'package:flutter_demo/ui/pages/composite/widgets/banner_pagination_builder.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class BannerWidget extends StatefulWidget {
  const BannerWidget(
      {super.key, required this.bannerModel, required this.itemHeight});

  final BannerModel bannerModel;
  final double itemHeight;

  @override
  State<BannerWidget> createState() => _BannerWidgetState();
}

class _BannerWidgetState extends State<BannerWidget> {
  late SwiperController controller;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.itemHeight,
      child: Stack(
        children: [
          Swiper(
              itemBuilder: (context, index) {
                return _buildItem(index);
              },
              index: 0,
              autoplay: widget.bannerModel.banner!.length > 1,
              autoplayDelay: 6000,
              //切换图片回调
              // onIndexChanged: (index) async {
              //   _updatePaletteGenerator(
              //       widget.bannerModel.banner![index].bannerId);
              // },
              //轮播指示器
              pagination: widget.bannerModel.banner!.length > 1
                  ? const SwiperPagination(
                      builder: BannerPaginationBuilder(
                          color: Colors.white,
                          size: Size(14, 3),
                          activeSize: Size(14, 3),
                          space: 4,
                          activeColor: Colors.red),
                    )
                  : null,
              controller: controller,
              itemCount: widget.bannerModel.banner!.length),
        ],
      ),
    );
  }

  @override
  void dispose() {
    controller.dispose();
    super.dispose();
  }

  @override
  void deactivate() {
    controller.stopAutoplay();
    super.deactivate();
  }

  @override
  void activate() {
    controller.startAutoplay();
    super.activate();
  }

  @override
  void initState() {
    super.initState();
    controller = SwiperController();
  }

  Widget _buildItem(int index) {
    final banner = widget.bannerModel.banner![index];
    return GestureDetector(
      onTap: () {
        logger.i("点击 banner");
      },
      child: Container(
        margin: EdgeInsets.fromLTRB(15.w, 5.w, 15.w, 0),
        child: ClipRRect(
          //圆角
          borderRadius: BorderRadius.circular(12.r),
          //图片缓存
          child: CachedNetworkImage(
            //图片地址
            imageUrl: banner.pic!,
            //图片切换时空白填充颜色
            placeholder: (context, url) {
              return Container(
                color: const Color.fromARGB(255, 245, 245, 245),
              );
            },
            imageBuilder: (context, imageProvider) {
              return Stack(
                children: [
                  //图片
                  Positioned.fill(
                    child: Image(
                      image: imageProvider,
                      fit: BoxFit.cover,
                    ),
                  ),
                  //图片提示文字
                  Positioned(
                    right: 0,
                    bottom: 0,
                    child: Container(
                        height: 17.h,
                        decoration: const BoxDecoration(
                            color: Color.fromARGB(255, 201, 61, 45),
                            borderRadius: BorderRadius.only(
                                topLeft: Radius.circular(12))),
                        padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
                        child: Center(
                          child: Text(
                            banner.title!,
                            style:
                                TextStyle(color: Colors.white, fontSize: 12.sp),
                          ),
                        )),
                  )
                ],
              );
            },
          ),
        ),
      ),
    );
  }
}
